import router from "../router";

import Vue from "vue";

import Vuex from "vuex";

Vue.use(Vuex);

import * as api from "../api";

import allRoutes from "../router/allRouters";//引入所有的路由配置

import dynamicRoutes from '../router/dynamicRoutes';//动态路由配置

import recursionRoutes from '../utils/recursionRoutes';//递归计算用户路由的方法


let userInfo = localStorage.getItem("qf-userInfo") || "{}";

let currentChargeProduct = localStorage.getItem("qf-currentChargeProduct") || "[]"


export default new Vuex.Store({
    state: {
        userInfo: JSON.parse(userInfo),//用户信息
        sideMenu: [],//侧边栏菜单
        breadCrumb: [],//面包屑数据
        dialogVisible: false,//控制dialog显示隐藏的值
        dialogTitle: "增加学员信息",//控制dialog的标题
        stuData: [],//
        loading: true,
        currentRow: {},
        stuTotal: 0,//总条目
        stuPageSize: 10,//每页显示的数量
        orderData: [],//订单
        currentChargeProduct: JSON.parse(currentChargeProduct),//当前充值的产品
        order_id: ""//订单id
    },
    getters: {},
    mutations: {
        SHOW_LOADING() {
            this.loading = false
        },
        HIDE_LOADING() {
            this.loading = true
        },
        //修改dialog回显数据
        SET_CURRENT_ROW(state, payload) {
            state.currentRow = { ...payload }
            console.log(payload);
        },
        //设置学员项目管理表格中的数据
        SET_STU_DATA(state, payload) {
            state.stuData = payload.data;//设置学员列表
            state.loading = false;
            state.stuTotal = payload.total;
        },
        //显示dialog
        SHOW_DIALOG(state) {
            state.dialogVisible = true
        },
        //隐藏dialog
        HIDE_DIALOG(state) {
            state.dialogVisible = false
        },
        //切换dialog标题信息
        CHANGE_DIALOG_TITLE(state, payload) {
            state.dialogTitle = payload;
        },
        //保存用户信息
        SET_USER_INFO(state, payload) {//设置用户信息
            state.userInfo = payload;
        },
        SET_MENULIST(state, payload) {//设置菜单
            // state.sideMenu = payload;
            // console.log(state.sideMenu);
            //将用户路由动态的添加到router的配置routes中\
            //将用户路由添加到dynamicRoutes中的"/"路径中配置的那个cheildren属性中
            // let target = dynamicRoutes.find(item => item.path === "/");
            // router.children = payload;
            let target = dynamicRoutes.find(item => item.path === "/")
            // console.log(target);
            target.children = [...payload, ...target.children]
            //要将用户路由动态的添加到我们的项目中
            // console.log(dynamicRoutes);
            router.addRoutes(dynamicRoutes);
            //核心方法就是router.addRoutes([])
            //预支付订单不需要展示在菜单栏里
            //要排除不需要展示在菜单中的页面 pre_order payMent
            let excludeRoutes = ["pre_order", "payment"]

            target.children = target.children.filter(item => !excludeRoutes.includes(item.path))

            state.sideMenu = target.children

        },
        SET_BREADCRUMB(state, payload) {//设置面包屑
            state.breadCrumb = payload;
        },
        SRTSTULIST(state, data) {
            // console.log(data);
            state.stuData = data
            this.loading = false
        },
        //设置订单
        SET_ORDER_DATA(state, payload) {
            state.orderData = payload
        },
        //设置当前充值的产品
        SET_CURRENT_CHARGE_PRODUCT(state, payload) {
            state.currentChargeProduct = [payload]
            //再存一份到localStorage中
            localStorage.setItem('qf-currentChargeProduct', JSON.stringify([payload]))
        },
        //设置订单id
        SET_ORDER_ID(state, payload) {
            state.order_id = payload;
        }


    },
    actions: {
        //获取用户菜单
        async GET_MENULIST({ commit }) {
            // console.log('action执行了');
            //用于获取服务器中当前用户的菜单
            let res = await api.getMenulistApi()
            // console.log(res);
            let { menuList } = res.data;
            // console.log(menuList);
            //使用recursionRoutes方法，计算出用户的路由，提交给mutation
            let userRoutes = recursionRoutes(menuList, allRoutes)
            commit("SET_MENULIST", userRoutes)
        },
        //获取学员列表
        // async GET_STULIST(context) {
        //     // let { page = 1, count = 15, classes = "" } = payload;//解构传递的参数
        //     let res = await api.getStuListApi()
        //     //    res.state?commit('SET_STU_DATA',res.data):null
        //     //    console.log(res.data.data);
        //     //    console.log(res);
        //     if (res.data.state) {
        //         context.commit('SRTSTULIST', res.data.data )
        //     }
        //     return res.data.state
        // }

        //获取学员列表
        GET_STULIST({ commit }, payload = {}) {
            // console.log(payload);
            commit("SHOW_LOADING")//打开加载动画
            let { page = 1, count = 10, classes = "" } = payload;//解构传递的参数
            api.getStuListApi(page, count, classes)
                .then(res => {
                    commit("SET_STU_DATA", res.data)
                })
        },
        //获取订单数据
        GET_ORDER_DATA({ commit }, payload) {
            let order_id = payload || "";
            api.getOrderApi(order_id)
                .then(res => {
                    console.log(res.data);
                    if (res.data.state) {
                        console.log(res.data.data);
                        commit("SET_ORDER_DATA", res.data.data)
                    } else {
                        this.$message.error('订单获取失败')
                    }
                })
        }
    },
    modules: {},//模块化
});